function getCookie(name)
{
    var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
    if(arr=document.cookie.match(reg)) return unescape(arr[2]);
    else return null;
}
function setCookie(c_name,value,expiredays)
{
	var exdate=new Date()
	exdate.setDate(exdate.getDate()+expiredays)
	document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
}
function addCom(_iid,_name,_nick)
{
	$.get("./carGet.php", {},
			function(data)
			{
				if(data.length>=4)
				{
					alert("最多允许4个商品对比!");
					return;
				}
				for(i=0;i<data.length;i++)
				{
					if(data[i][0]==_iid)
					{
						alert("对不起，此商品已添加！");
						return;
					}
				}
			},
			"json" 
		 );
	$.get("./carGet.php", { iid:_iid , name:_name,nick:_nick,act:"add" },
		function(data){showCom(data);},
		"json" );
}
function delCom(_iid)
{
	$.get("./carGet.php",{iid:_iid,act:"del"},function(data){showCom(data);},"json");
}
function showCom(items)
{
	if(items.length>0)
	{
		str ="<ul style='display: block;'>\n";
		for(i=0;i<items.length;i++)
		{
			str += "<li>"+items[i][1].substr(0,30)+" <a href='javascript:' onclick=\"delCom('"+items[i][0]+"')\">删除</a></li>\n"
		}
		str += "<a id='start_com' href='javascript:' onclick='startCom()'>开始对比</a>\n";
		str += "<ul>";
		$("#comDiv").html(str);
		$("#comDiv").css("display","");
	}else
	{
		$("#comDiv").css("display","none");
	}
}
function loadCom()
{
	$.get("./carGet.php", {},
			function(data){showCom(data);},
			"json" 
		 );
}
function startCom()
{
	$.get("./carGet.php", {},
			function(data)
			{
				if(data.length<2)
				{
					alert("对不起，最少选择2件商品进行对比！");
					return;
				}else
				{
					hrefStr = "./comItems.php?iid=";
					for(i=0;i<data.length;i++)
					{
						hrefStr += data[i][0]+"-"+data[i][2]+",";
					}
					hrefStr = hrefStr.substr(0,hrefStr.length-1);
					window.open(hrefStr);
				}
			},
			"json" 
		 );
}